package cn.iocoder.yudao.framework.common.exception;

import cn.iocoder.yudao.framework.common.exception.enums.ServiceErrorCodeRange;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * FIXME 重构
 * 业务逻辑异常 Exception
 */
@Data
public class ServiceException extends RuntimeException {

    /**
     * 业务错误码
     *
     * @see cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants
     * @see cn.iocoder.yudao.framework.common.exception.enums.ServiceErrorCodeRange
     */
    private Integer code;
    /**
     * 错误提示
     */
    private String message;

    /**
     * 空构造方法，避免反序列化问题
     */
    public ServiceException() {
        super("业务异常");
        this.code = ServiceErrorCodeRange.BIZ_EXCEPTION;
        this.message = "业务异常";
    }

    public ServiceException(Throwable cause) {
        super(cause.getMessage());
        this.code = ServiceErrorCodeRange.BIZ_EXCEPTION;
        this.message = "业务异常";
    }

    public ServiceException(ErrorCode errorCode) {
        super(errorCode.getMsg());
        this.code = errorCode.getCode();
        this.message = errorCode.getMsg();
    }

    public ServiceException(String message) {
        this(ServiceErrorCodeRange.BIZ_EXCEPTION, message);
    }

    public ServiceException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public <T extends ServiceException> T setCode(Integer code) {
        this.code = code;
        //noinspection unchecked
        return (T) this;
    }

    public <T extends ServiceException> T setMessage(String message) {
        this.message = message;
        //noinspection unchecked
        return (T) this;
    }

}
